import h5py as h5
from template import *

f = h5.File("Mesh_2.cgns")

#print([key for key in f.keys()])
#print(f[" format"][:])
#print(f[" hdf5version"][:])
#print(f["CGNSLibraryVersion"])

list_keys = list(f.keys())
print("list_keys: ", list_keys)

mesh_name = list_keys[3]
print("mesh name: ", mesh_name)

hdf5_version = f[" hdf5version"]
print("hdf5_version: ", [chr(key) for key in hdf5_version[:]])





mesh_1 = f["Mesh_1"]
print("mesh_1 keys: ", [key for key in mesh_1.keys()])

data = mesh_1[" data"]
#print("vertex and element number: ", data[:])


edge_element = list(f["Mesh_1"]["SMESH_Mesh"].keys())[3]
tri_element  = list(f["Mesh_1"]["SMESH_Mesh"].keys())[4]
print("edge_element: ", edge_element)
print("tri_element: ", tri_element)


print("--------------zone-----------------------------")
smesh_mesh = mesh_1["SMESH_Mesh"]
print("smesh_mesh keys: ",[key for key in smesh_mesh.keys()])
print("--------------vertex-----------------------------")
vertex_x = smesh_mesh["GridCoordinates"]["CoordinateX"][" data"][:]
vertex_y = smesh_mesh["GridCoordinates"]["CoordinateY"][" data"][:]
vertex_z = smesh_mesh["GridCoordinates"]["CoordinateZ"][" data"][:]
for i in range(0, vertex_x.shape[0]):
    print("vertex ", i, vertex_x[i], vertex_y[i], vertex_z[i])




print("--------------boundary elements-----------------------------")
#print("boundary keys: ", list(smesh_mesh[edge_element].keys()))
#print("boundary element range: ", smesh_mesh[edge_element]["ElementRange"][" data"][:])
#print("boundary element connectivity: ", smesh_mesh[edge_element]["ElementConnectivity"][" data"][:])
bar_elements = smesh_mesh[edge_element]["ElementConnectivity"][" data"][:]
bar_elements = bar_elements.reshape((int(bar_elements.shape[0]/2), 2)) - 1
for i in range(0, bar_elements.shape[0]):
    print("bar ", i, bar_elements[i, 0], bar_elements[i, 1]) 

print("--------------tri elements-----------------------------")
#print("boundary keys: ", list(smesh_mesh[tri_element].keys()))
#print("boundary element range: ", smesh_mesh[tri_element]["ElementRange"][" data"][:])
#print("boundary element connectivity: ", smesh_mesh[tri_element]["ElementConnectivity"][" data"][:])
tri_elements = smesh_mesh[tri_element]["ElementConnectivity"][" data"][:]
tri_elements = tri_elements.reshape((int(tri_elements.shape[0]/3), 3)) - 1
for i in range(0, tri_elements.shape[0]):
    print("tri ", i, tri_elements[i, 0], tri_elements[i, 1], tri_elements[i, 2])

print("--------------boundary-----------------------------")
zonebc = smesh_mesh["ZoneBC"]
print([key for key in zonebc.keys()])

zonebc_left = zonebc["right"]
print("left: ", [key for key in zonebc_left.keys()])
print("left data: ", [chr(key) for key in zonebc_left[" data"][:]])

PointList = zonebc_left["PointList"]
print("left PointList: ", PointList[" data"][:].shape, PointList[" data"][:] - 1)

GridLocation = zonebc_left["GridLocation"]
print("left GridLocation: ", [chr(key) for key in GridLocation[" data"][:]])




tri_x = f["Mesh_1"]["SMESH_Mesh"]["GridCoordinates"]["CoordinateX"][" data"][:]
tri_y = f["Mesh_1"]["SMESH_Mesh"]["GridCoordinates"]["CoordinateY"][" data"][:]
tri_range = f["Mesh_1"]["SMESH_Mesh"][tri_element]["ElementRange"][" data"][:]
tri_number = tri_range[1] - tri_range[0] + 1
tri_indexs = f["Mesh_1"]["SMESH_Mesh"][tri_element]["ElementConnectivity"][" data"][:]
tri_indexs = tri_indexs.reshape((tri_number, 3)) - 1

print("tri_x: ", tri_x)
print("tri_y: ", tri_y)
print("tri_indexs: ", tri_indexs)


#read data
data_file = h5.File("data.h5")

field = data_file["/fields/u"][:]

##inite the fig of matplotlib
fig=plt.figure(figsize=(10,8))
fig.subplots_adjust(top=0.9,bottom=0.1,wspace=0.5,hspace=0.4)


ax0 = fig.add_subplot(1,1,1)

ax0.triplot(tri_x, tri_y, tri_indexs, lw=1.0)

ax0.triconrf(tri_x, tri_y, field)

ax0.set_aspect('equal')

fig.savefig("mesh.png", dpi = 300)
plt.show()